<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>接收arp请求并发送回应的实例 - whowin - 发表我个人原创作品的技术博客</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="whowin" /><meta name="description" content="ARP协议在网络编程中有着重要的地位，是一个工作在数据链路层的协议，本文简单介绍了ARP协议，用一个实例查看收到的ARP请求，并对该请求发出ARP回应，实例有完整的源代码，使用C语言在Linux下实现，代码中有详细的注释；阅读本文需要有一定的网络编程基础，了解OSI的网络模型，本文对初学者有一定的难度。
" /><meta name="keywords" content="linux, socket, hugo, dos" />






<meta name="generator" content="Hugo 0.97.3 with theme even" />


<link rel="canonical" href="https://whowin.gitee.io/post/blog/network/0007-recv-arp-request-and-reply/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.e3fea119b1980e848b03dffbeddb11dd0fba483eed0e5f11870fb8e31f145bbd.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="接收arp请求并发送回应的实例" />
<meta property="og:description" content="ARP协议在网络编程中有着重要的地位，是一个工作在数据链路层的协议，本文简单介绍了ARP协议，用一个实例查看收到的ARP请求，并对该请求发出ARP回应，实例有完整的源代码，使用C语言在Linux下实现，代码中有详细的注释；阅读本文需要有一定的网络编程基础，了解OSI的网络模型，本文对初学者有一定的难度。" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://whowin.gitee.io/post/blog/network/0007-recv-arp-request-and-reply/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2023-01-12T16:43:29+08:00" />
<meta property="article:modified_time" content="2023-01-12T16:43:29+08:00" />

<meta itemprop="name" content="接收arp请求并发送回应的实例">
<meta itemprop="description" content="ARP协议在网络编程中有着重要的地位，是一个工作在数据链路层的协议，本文简单介绍了ARP协议，用一个实例查看收到的ARP请求，并对该请求发出ARP回应，实例有完整的源代码，使用C语言在Linux下实现，代码中有详细的注释；阅读本文需要有一定的网络编程基础，了解OSI的网络模型，本文对初学者有一定的难度。"><meta itemprop="datePublished" content="2023-01-12T16:43:29+08:00" />
<meta itemprop="dateModified" content="2023-01-12T16:43:29+08:00" />
<meta itemprop="wordCount" content="5201">
<meta itemprop="keywords" content="Linux,Socket,网络编程,arp,raw socket," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="接收arp请求并发送回应的实例"/>
<meta name="twitter:description" content="ARP协议在网络编程中有着重要的地位，是一个工作在数据链路层的协议，本文简单介绍了ARP协议，用一个实例查看收到的ARP请求，并对该请求发出ARP回应，实例有完整的源代码，使用C语言在Linux下实现，代码中有详细的注释；阅读本文需要有一定的网络编程基础，了解OSI的网络模型，本文对初学者有一定的难度。"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

  <script async src="/js/busuanzi.pure.mini.js"></script><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9724909319263152"
     crossorigin="anonymous"></script>


</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">WhoWin</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">文章归档</li>
      </a><a href="/article-categories/categories/">
        <li class="mobile-menu-item">文章分类</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">文章标签</li>
      </a><a href="/about/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">WhoWin</a>
  
  <div style="position:absolute; left: 80px; top: 75px; color: crimson">
      ———开源和分享是技术发展的源泉和动力；本博客所有文章均为原创
  </div>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">文章归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/article-categories/categories/">文章分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">文章标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">接收arp请求并发送回应的实例</h1>

      <div class="post-meta">
        <span class="post-time"> 2023-01-12 </span>
        <div class="post-category">
            <a href="/categories/linux/"> Linux </a>
            <a href="/categories/c-language/"> C Language </a>
            <a href="/categories/network/"> Network </a>
            </div>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#1-arp协议">1. ARP协议</a></li>
        <li><a href="#2-arp请求回应的发送和接收">2. ARP请求/回应的发送和接收</a></li>
        <li><a href="#3-接收arp请求并发送回应的实例">3. 接收ARP请求并发送回应的实例</a></li>
        <li><a href="#欢迎订阅-网络编程专栏httpsblogcsdnnetwhowincategory_12180345html"><strong>欢迎订阅 <a href="https://blog.csdn.net/whowin/category_12180345.html">『网络编程专栏』</a></strong></a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <p>ARP协议在网络编程中有着重要的地位，是一个工作在数据链路层的协议，本文简单介绍了ARP协议，用一个实例查看收到的ARP请求，并对该请求发出ARP回应，实例有完整的源代码，使用C语言在Linux下实现，代码中有详细的注释；阅读本文需要有一定的网络编程基础，了解OSI的网络模型，本文对初学者有一定的难度。</p>
<h2 id="1-arp协议">1. ARP协议</h2>
<ul>
<li>
<p>ARP(Address Resolution Protocol)，地址解析协议；在局域网上通过IP地址获取物理地址MAC的协议，该协议工作在数据链路层；</p>
</li>
<li>
<p>为什么需要ARP协议</p>
<ul>
<li>以TCP/IP协议为例，应用程序在应用层发出信息后，在传输层(TCP层)加上一个TCP报头，TCP报头中需要填写源端口和目的端口，端口号标识着一台机器上的某个确定的应用程序，在网络层(IP层)加上一个IP报头，IP报头需要填写源IP地址和目的IP地址，IP地址标识着互联网上一台唯一的机器，所以，TCP报头和IP报头可以确定下来互联网上的某台机器上的某个应用程序；</li>
<li>在数据链路层需要给数据包加上以太网报头，在以太网报头中，需要填的是机器的物理地址(MAC地址)，源地址和目的地址均要填MAC地址；</li>
<li>在局域网中传输数据，是要依靠数据链层中的MAC地址的，我们可以使用ioctl获取本机的MAC地址，但是却无法获取目的地址的MAC地址，这时就需要使用ARP协议了；</li>
</ul>
</li>
<li>
<p>ARP的工作原理</p>
<ul>
<li>在以太网络中，每台机器均有一个ARP cache，里面存着一些IP地址和MAC地址的对应关系，在ubuntu下，内核把ARP cache映射为文件：<code>/proc/net/arp</code>，所以使用 <code>cat /proc/net/arp</code> 是可以看到本机的 ARP cache 的</li>
<li>在数据链路层需要填MAC地址时，首先查询ARP cache中是否有相应的记录，如果有则直接使用；</li>
<li>如果在ARP cache中没有记录，则向局域网广播一个ARP请求，里面有需要获取MAC地址的机器所对应的IP地址，当这个IP地址收到这个ARP请求时，则会回应一个ARP回应，将自己的MAC地址告知发送请求的机器，发出请求的机器收到该回应后，应该在ARP cache中添加上这条记录；</li>
<li>有关ARP cache的操作，请参考文章<a href="https://blog.csdn.net/whowin/article/details/129791465">《如何用C语言操作arp cache》</a>。</li>
</ul>
</li>
<li>
<p>ARP请求/回应的格式</p>
<ul>
<li>对一个ARP请求的回应被称为ARP回应，ARP请求数据包和回应数据包的结构是一样的；</li>
</ul>
<p><img src="https://whowin.gitee.io/images/180007/arp_packet.png" alt="arp request"></p>
</li>
</ul>
<hr>
<ul>
<li>
<p><strong>以太网报头(Ethernet Header)</strong></p>
<ul>
<li>数据链路层的以太网报头定义在头文件&lt;linux/if_ether.h&gt;中：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">ethhdr</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">char</span>  <span class="n">h_dest</span><span class="p">[</span><span class="n">ETH_ALEN</span><span class="p">];</span>    <span class="cm">/* destination eth addr  */</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">char</span>  <span class="n">h_source</span><span class="p">[</span><span class="n">ETH_ALEN</span><span class="p">];</span>  <span class="cm">/* source ether addr  */</span>
</span></span><span class="line"><span class="cl">    <span class="n">__be16</span>         <span class="n">h_proto</span><span class="p">;</span>             <span class="cm">/* packet type ID field  */</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span> <span class="n">__attribute__</span><span class="p">((</span><span class="n">packed</span><span class="p">));</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>上面的ETH_ALAN的值为6，定义在头文件linux/if_ether.h中；</li>
<li>h_dest字段为目的MAC地址，h_source字段为源MAC地址；</li>
<li>h_proto表示当前数据包在网络层使用的协议，Linux支持的协议在头文件linux/if_ether.h中定义；通常在网络层使用的IP协议，这个字段的值是0x0800(宏ETH_P_IP)，<strong>对于ARP报文，这个字段的值为0x0806(ETH_P_ARP)</strong>，表示网络层使用的是ARP协议；</li>
<li>对于一个ARP请求数据包来说，h_dest通常为6个0xff，这是因为发送者并不知道目的机器的MAC地址，发送者的意图就是通过一个ARP请求包获得目的机器的MAC地址，所以发送者需要把这个数据包广播出去，使每台机器都可以收到，6个0xff表示一个广播地址；</li>
<li>对于ARP请求的数据包来说，h_source是发送者自己的MAC地址，发送者在发送ARP请求时，自己的MAC地址是显然已知的，如果不知道，可以通过ioctl()获取(请参考文章<a href="https://blog.csdn.net/whowin/article/details/128891255">《如何使用raw socket发送UDP报文》</a>)；</li>
<li>对于ARP回应的数据包来说，目的地址就是发送ARP请求包的机器，h_dest应该就是收到的ARP请求包中的h_source；</li>
<li>对于ARP回应的数据包来说，h_source就是自己的MAC地址，这个当然是已知的；</li>
<li>不管是ARP请求还是回应数据包，h_proto都是0x0806(宏ETH_P_ARP)，表示网络层使用的协议是ARP协议</li>
</ul>
</li>
<li>
<p><strong>ARP报头(ARP header)</strong></p>
<p><img src="https://whowin.gitee.io/images/180007/arp_header.png" alt="ARP header"></p>
<hr>
<ul>
<li>arp报头定义在头文件linux/if_arp.h中：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">arphdr</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">__be16</span>          <span class="n">ar_hrd</span><span class="p">;</span>       <span class="cm">/* format of hardware address   */</span>
</span></span><span class="line"><span class="cl">    <span class="n">__be16</span>          <span class="n">ar_pro</span><span class="p">;</span>       <span class="cm">/* format of protocol address   */</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">char</span>   <span class="n">ar_hln</span><span class="p">;</span>       <span class="cm">/* length of hardware address   */</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">char</span>   <span class="n">ar_pln</span><span class="p">;</span>       <span class="cm">/* length of protocol address   */</span>
</span></span><span class="line"><span class="cl">    <span class="n">__be16</span>          <span class="n">ar_op</span><span class="p">;</span>        <span class="cm">/* ARP opcode (command)         */</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>ar_hrd字段为ARP协议硬件类型，相关定义在&lt;linux/if_arp.h&gt;中，对以太网而言，硬件类型为1(宏ARPHRD_ETHER)；</li>
<li>ar_pro字段为协议类型，协议类型定义在linux/if_ether.h中，IP协议的协议类型为0x800(宏ETH_P_IP)；</li>
<li>ar_hln字段为硬件地址的长度，对以太网而言，硬件地址就是MAC地址，长度是6 bytes(宏ETH_ALEN)；</li>
<li>ar_pln字段为协议地址长度，对IP协议的IPv4而言，协议地址就是IP地址，长度是4 bytes；</li>
<li>ar_op字段表示要执行的操作，1-ARP请求，2-ARP回应；</li>
<li>在发送ARP回应时，ARP报头比较好处理，只需要把ar_op从1(宏ARROP_REQUEST)改成2(ARROP_REPLY)即可，其它字段均不需要改动。</li>
</ul>
</li>
<li>
<p><strong>ARP Payload</strong></p>
<p><img src="https://whowin.gitee.io/images/180007/arp_payload.png" alt="ARP payload"></p>
<hr>
<ul>
<li>对于以太网IPv4而言，ARP payload的定义如下
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">eth_arpmsg</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">char</span>   <span class="n">arp_sha</span><span class="p">[</span><span class="n">ETH_ALEN</span><span class="p">];</span>   <span class="cm">/* Sender Hardware Address(MAC) */</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">char</span>   <span class="n">arp_spa</span><span class="p">[</span><span class="mi">4</span><span class="p">];</span>          <span class="cm">/* Sender Protocol Address(IP) */</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">char</span>   <span class="n">arp_dha</span><span class="p">[</span><span class="n">ETH_ALEN</span><span class="p">];</span>   <span class="cm">/* Destination Hardware Address(MAC) */</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">char</span>   <span class="n">arp_dpa</span><span class="p">[</span><span class="mi">4</span><span class="p">];</span>          <span class="cm">/* Destination Protocol Address(IP) */</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>ARP payload的实际长度要根据ARP报头来确定，对于以太网IPv4而言，其结构是可以确定的，但对于其它网络而言，硬件地址的表达形式可能不是MAC地址，那么sender hardware address的长度也有可能不是6；协议地址的长度也是一样，比如IPv6的协议地址长度就是128位，16个字节；所以，如果不是以太网，则arp payload是要重新定义的，好在我们日常见到的网络都是以太网IPv4；</li>
<li>对于ARP请求数据包而言，arp_sha和arp_spa为发送者的MAC地址和IP地址，发送者是已知的，接收者的IP也是知道的，所以arp_dpa字段也是填好的，但接收者的MAC地址，也就是arp_dha字段发送者并不知道(发送者发送ARP请求就是为了获得这个MAC)，所以收到的ARP请求包里arp_dha字段为6个0；</li>
<li>在回应ARP请求包时，ARP请求包的发送者成为了接收者，arp_dha和arp_dpa(目的机器的MAC和IP)应该就是请求包中的arp_sha和arp_spa；</li>
<li>在ARP回应数据包中，arp_sha和arp_spa就是本机的MAC地址和IP地址，这些信息都是已知的。</li>
</ul>
</li>
</ul>
<h2 id="2-arp请求回应的发送和接收">2. ARP请求/回应的发送和接收</h2>
<ul>
<li>对于习惯于使用socket基于TCP/IP协议的编程来说，arp的请求既无法使用TCP socket(SOCK_STREAM)发送，也无法使用UDP socket(SOCK_DGRAM)发送；接收也是一样，TCP/UDP socket是无法收到ARP的回应信息的；</li>
<li>前面说过，ARP协议是工作在数据链路层的，而TCP/UDP socket是用在应用层的，应用层发送的数据在传输层要被加上一个TCP/UDP头，在网络层要被加上一个IP头，在数据链路层要被加上一个以太网头，然后才交给物理层(网卡的驱动程序)发送；</li>
<li>相关概念请参考<a href="https://blog.csdn.net/whowin/article/details/128766145">《Linux下如何在数据链路层接收原始数据包》</a>和<a href="https://blog.csdn.net/whowin/article/details/128891255">《如何使用raw socket发送UDP报文》</a></li>
<li>发送ARP请求、接收ARP回应只能在数据链路层完成，要在数据链路层上编程，需要用到raw socket，相关的知识也请参考上面两篇文章；</li>
<li>在发送ARP请求时，封装过程与文章<a href="https://blog.csdn.net/whowin/article/details/128891255">《如何使用raw socket发送UDP报文》</a>中类似；</li>
<li>在接收ARP回应时，拆包过程与文章<a href="https://blog.csdn.net/whowin/article/details/128766145">《Linux下如何在数据链路层接收原始数据包》</a>中类似；</li>
<li>使用程序获取、添加和删除ARP cache记录的方法，请参考文章<a href="https://blog.csdn.net/whowin/article/details/129791465">《如何用C语言操作arp cache》</a></li>
<li>关于如何构建以太网头、ARP报头和ARP payload，在上一节介绍以太网报头和ARP报文时已经做了介绍。</li>
</ul>
<h2 id="3-接收arp请求并发送回应的实例">3. 接收ARP请求并发送回应的实例</h2>
<ul>
<li>
<p>在这个例子中，程序接收局域网上所有机器发出的arp请求包，如果这个请求包是发给本机的，即arp请求包中的目的IP为本机IP，则显示收到的以太网报头和arp报文，并向发送者发送一个arp回应；</p>
</li>
<li>
<p>程序的运行步骤如下：</p>
<ol>
<li>创建一个raw socket</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="kt">int</span> <span class="n">sock_raw</span> <span class="o">=</span> <span class="n">socket</span><span class="p">(</span><span class="n">AF_PACKET</span><span class="p">,</span> <span class="n">SOCK_RAW</span><span class="p">,</span> <span class="n">htons</span><span class="p">(</span><span class="n">ETH_P_ALL</span><span class="p">));</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="2">
<li>查询网络接口的索引号(interface index)</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="cp">#define DEVICE    &#34;enp0s3&#34;
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="k">struct</span> <span class="n">ifreq</span> <span class="n">ifr</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">memset</span><span class="p">(</span><span class="o">&amp;</span><span class="n">ifr</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">ifreq</span><span class="p">));</span>
</span></span><span class="line"><span class="cl"><span class="n">strncpy</span><span class="p">(</span><span class="n">ifr</span><span class="p">.</span><span class="n">ifr_name</span><span class="p">,</span> <span class="n">DEVICE</span><span class="p">,</span> <span class="n">IFNAMSIZ</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">ioctl</span><span class="p">(</span><span class="n">sock_raw</span><span class="p">,</span> <span class="n">SIOCGIFINDEX</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">ifr</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="n">ifindex</span> <span class="o">=</span> <span class="n">ifr</span><span class="p">.</span><span class="n">ifr_ifindex</span><span class="p">;</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="3">
<li>查询本机的MAC地址</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">src_mac</span><span class="p">[</span><span class="n">ETH_ALEN</span><span class="p">];</span>
</span></span><span class="line"><span class="cl"><span class="n">ioctl</span><span class="p">(</span><span class="n">sock_raw</span><span class="p">,</span> <span class="n">SIOCGIFHWADDR</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">ifr</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">memcpy</span><span class="p">(</span><span class="n">src_mac</span><span class="p">,</span> <span class="n">ifr</span><span class="p">.</span><span class="n">ifr_hwaddr</span><span class="p">.</span><span class="n">sa_data</span><span class="p">,</span> <span class="n">ETH_ALEN</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="4">
<li>查询本机的IP地址</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="cp">#define DEVICE    &#34;enp0s3&#34;
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="k">struct</span> <span class="n">ifreq</span> <span class="n">ifr</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">memset</span><span class="p">(</span><span class="o">&amp;</span><span class="n">ifr</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">ifreq</span><span class="p">));</span>
</span></span><span class="line"><span class="cl"><span class="n">strncpy</span><span class="p">(</span><span class="n">ifr</span><span class="p">.</span><span class="n">ifr_name</span><span class="p">,</span> <span class="n">DEVICE</span><span class="p">,</span> <span class="n">IFNAMSIZ</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="k">if</span> <span class="p">(</span><span class="n">ioctl</span><span class="p">(</span><span class="n">sock_raw</span><span class="p">,</span> <span class="n">SIOCGIFADDR</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">ifr</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">printf</span><span class="p">(</span><span class="s">&#34;SIOCGIFADDR.</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="n">close</span><span class="p">(</span><span class="n">sock_raw</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="n">exit</span><span class="p">(</span><span class="n">EXIT_FAILURE</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="kt">uint32_t</span> <span class="n">src_ip</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">memcpy</span><span class="p">((</span><span class="kt">void</span> <span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">src_ip</span><span class="p">,</span> <span class="o">&amp;</span><span class="p">(((</span><span class="k">struct</span> <span class="n">sockaddr_in</span> <span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="p">(</span><span class="n">ifr</span><span class="p">.</span><span class="n">ifr_addr</span><span class="p">))</span><span class="o">-&gt;</span><span class="n">sin_addr</span><span class="p">),</span> <span class="k">sizeof</span><span class="p">(</span><span class="kt">uint32_t</span><span class="p">));</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="5">
<li>为发送arp报文设置sockaddr_ll</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">sockaddr_ll</span> <span class="n">saddr_ll</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">memset</span><span class="p">((</span><span class="kt">void</span> <span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">saddr_ll</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">sockaddr_ll</span><span class="p">));</span>
</span></span><span class="line"><span class="cl"><span class="n">saddr_ll</span><span class="p">.</span><span class="n">sll_family</span>   <span class="o">=</span> <span class="n">PF_PACKET</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">saddr_ll</span><span class="p">.</span><span class="n">sll_protocol</span> <span class="o">=</span> <span class="n">htons</span><span class="p">(</span><span class="n">ETH_P_IP</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">saddr_ll</span><span class="p">.</span><span class="n">sll_ifindex</span>  <span class="o">=</span> <span class="n">ifindex</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">saddr_ll</span><span class="p">.</span><span class="n">sll_hatype</span>   <span class="o">=</span> <span class="n">ARPHRD_ETHER</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">saddr_ll</span><span class="p">.</span><span class="n">sll_pkttype</span>  <span class="o">=</span> <span class="n">PACKET_OTHERHOST</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">saddr_ll</span><span class="p">.</span><span class="n">sll_halen</span>    <span class="o">=</span> <span class="n">ETH_ALEN</span><span class="p">;</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="6">
<li>等待接收来自网络的数据包</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="k">struct</span> <span class="nf">__attribute__</span><span class="p">((</span><span class="n">packed</span><span class="p">))</span> <span class="n">arp_packet</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">struct</span> <span class="n">arphdr</span> <span class="n">arp_hdr</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">arp_sha</span><span class="p">[</span><span class="n">ETH_ALEN</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">arp_spa</span><span class="p">[</span><span class="mi">4</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">arp_dha</span><span class="p">[</span><span class="n">ETH_ALEN</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">    <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">arp_dpa</span><span class="p">[</span><span class="mi">4</span><span class="p">];</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="n">buf_size</span> <span class="o">=</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">ethhdr</span><span class="p">)</span> <span class="o">+</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">arp_packet</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="kt">void</span> <span class="o">*</span><span class="n">buffer</span> <span class="o">=</span> <span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="n">buf_size</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">memset</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">buf_size</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">recvfrom</span><span class="p">(</span><span class="n">sock_raw</span><span class="p">,</span> <span class="n">buffer</span><span class="p">,</span> <span class="n">buf_size</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="7">
<li>如果数据包不是arp数据包，则忽略，回到步骤6</li>
<li>如果数据包不是arp请求包，则忽略，回到步骤6</li>
<li>如果arp请求包不是发给本机的，则忽略，回到步骤6</li>
<li>显示以太网报头</li>
<li>显示arp数据包(arp header + arp payload)</li>
<li>构建用于发送arp reply的以太网报头</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="n">eth_header</span> <span class="o">=</span> <span class="n">buffer</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">memcpy</span><span class="p">((</span><span class="kt">void</span> <span class="o">*</span><span class="p">)</span><span class="n">eth_header</span><span class="p">,</span> <span class="p">(</span><span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="p">)(</span><span class="n">eth_header</span> <span class="o">+</span> <span class="n">ETH_ALEN</span><span class="p">),</span> <span class="n">ETH_ALEN</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">memcpy</span><span class="p">((</span><span class="kt">void</span> <span class="o">*</span><span class="p">)(</span><span class="n">eth_header</span> <span class="o">+</span> <span class="n">ETH_ALEN</span><span class="p">),</span> <span class="p">(</span><span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="p">)</span><span class="n">src_mac</span><span class="p">,</span> <span class="n">ETH_ALEN</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">ethhdr</span> <span class="o">*</span><span class="n">eh</span> <span class="o">=</span> <span class="p">(</span><span class="k">struct</span> <span class="n">ethhdr</span> <span class="o">*</span><span class="p">)</span><span class="n">buffer</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">eh</span><span class="o">-&gt;</span><span class="n">h_proto</span> <span class="o">=</span> <span class="n">ETH_P_ARP</span><span class="p">;</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="13">
<li>构建用于发送arp reply的arp数据包</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="n">arp_header</span> <span class="o">=</span> <span class="n">buffer</span> <span class="o">+</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">ethhdr</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">arp_packet</span> <span class="o">*</span><span class="n">ah</span> <span class="o">=</span> <span class="p">(</span><span class="k">struct</span> <span class="n">arp_packet</span> <span class="o">*</span><span class="p">)</span><span class="n">arp_header</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">ah</span><span class="o">-&gt;</span><span class="n">arp_hdr</span><span class="p">.</span><span class="n">ar_op</span> <span class="o">=</span> <span class="n">ARPOP_REPLY</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">memcpy</span><span class="p">(</span><span class="n">ah</span><span class="o">-&gt;</span><span class="n">arp_dha</span><span class="p">,</span> <span class="n">ah</span><span class="o">-&gt;</span><span class="n">arp_sha</span><span class="p">,</span> <span class="n">ETH_ALEN</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">memcpy</span><span class="p">(</span><span class="n">ah</span><span class="o">-&gt;</span><span class="n">arp_dpa</span><span class="p">,</span> <span class="n">ah</span><span class="o">-&gt;</span><span class="n">arp_spa</span><span class="p">,</span> <span class="mi">4</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">memcpy</span><span class="p">(</span><span class="n">ah</span><span class="o">-&gt;</span><span class="n">arp_sha</span><span class="p">,</span> <span class="n">src_mac</span><span class="p">,</span> <span class="n">ETH_ALEN</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="n">memcpy</span><span class="p">(</span><span class="n">ah</span><span class="o">-&gt;</span><span class="n">arp_spa</span><span class="p">,</span> <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">src_ip</span><span class="p">,</span> <span class="mi">4</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="14">
<li>将sockaddr_ll中的sll_addr字段填为目的MAC地址</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="n">memcpy</span><span class="p">(</span><span class="n">saddr_ll</span><span class="p">.</span><span class="n">sll_addr</span><span class="p">,</span> <span class="n">eh</span><span class="o">-&gt;</span><span class="n">h_dest</span><span class="p">,</span> <span class="n">ETH_ALEN</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="15">
<li>显示用于发送arp reply的以太网报头</li>
<li>显示用于发送arp reply的arp数据包内容</li>
<li>发送arp reply</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C" data-lang="C"><span class="line"><span class="cl"><span class="n">sendto</span><span class="p">(</span><span class="n">sock_raw</span><span class="p">,</span> <span class="n">buffer</span><span class="p">,</span> <span class="n">buf_size</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="p">(</span><span class="k">struct</span> <span class="n">sockaddr</span> <span class="o">*</span><span class="p">)</span><span class="o">&amp;</span><span class="n">saddr_ll</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">sockaddr_ll</span><span class="p">));</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="18">
<li>回到步骤6</li>
</ol>
</li>
<li>
<p>该程序的主体在一个死循环中，所以程序只能使用ctrl+c退出，为了保证退出时能很好地清理现场，程序中拦截了SIGINT信号，当按下ctrl+c时，程序将收到这个信号，进行现场清理并退出；</p>
</li>
<li>
<p>还是要简单讨论一下构建发送arp reply的arp数据包的过程：</p>
<ol>
<li>当收到一个arp请求时，其中的arp头部分基本不需要变，唯一要变的ar_op字段，因为这个字段在收到的数据包里为1，意即这是arp request，这个字段要改为2，意即这是一个arp reply</li>
<li>收到的arp报文中arp_sha为发送人的MAC，发送arp reply时，这个MAC应该成为收件人MAC，即：arp_dha，所以要将arp_sha拷贝到arp_dha</li>
<li>收到的arp报文中arp_spa为发送人的IP，发送arp reply时，这个IP应该成为收件人IP，即：arp_dpa，所以要将arp_spa拷贝到arp_dpa</li>
<li>发送arp reply时，发件人MAC，也就是arp_sha应该为本机的MAC</li>
<li>发送arp reply时，发件人IP，也就是arp_spa应该为本机IP</li>
</ol>
</li>
<li>
<p>构建以太网报头的过程：</p>
<ol>
<li>收到的以太网报头中的收件人的MAC(h_dest)，应该为6个0xff，因为发送者发送arp request时并不知道接收者的MAC，所以发的是广播报文；</li>
<li>收到的以太网报头中的发件人的MAC(h_source)，在发送arp reply时应该为以太网报头中的收件人的MAC(h_dest)；</li>
<li>发送arp reply时的发件人的MAC应该填本机的MAC；</li>
</ol>
</li>
<li>
<p>由于ARP工作在数据链路层，所以不能使用普通的socket进行接收和发送，需要使用raw socket，使用raw socket时需要使用一个结构(struct sockaddr_ll)，这个结构的作用类似于IPv4的socket编程中的(struct sockaddr_in)的作用，有关这个结构的简单介绍，可以参考文章<a href="https://blog.csdn.net/whowin/article/details/128891255">《如何使用raw socket发送UDP报文》</a></p>
</li>
<li>
<p>由于这个程序是使用raw socket在数据链路层上接收数据，所以实际上程序可以收到在局域网上的所有数据包，程序过滤掉了不是ARP协议的数据包，然后又过滤掉了目的IP不是本机的数据包，在ctrl+c退出时，程序会显示收到了多少数据包，其中ARP协议数据包有多少，ARP请求包有多少，程序一共回应了多少个数据包，数据还是有点意思的；</p>
</li>
<li>
<p>下面是源程序，文件名：<a href="https://gitee.com/whowin/whowin/blob/blog/sourcecodes/180007/arp-request-and-reply.c">arp-request-and-reply.c</a>(<strong>点击文件名下载源程序</strong>)</p>
</li>
<li>
<p>编译，程序在ubuntu 20.04下编译通过，gcc版本为9.4.0</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">gcc -Wall arp-request-and-reply.c -o arp-request-and-reply
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>运行，由于使用了raw socket，必须要要有root权限才可以运行</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">sudo ./arp-request-and-reply
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>如果看不到有发给本机的arp请求，可以将你的手机连接到你得电脑所在的局域网上，然后，在你的电脑上开一个新的终端，在新的终端上ping一下你手机的IP，通常会激活手机向你的机器发送一个arp请求，当然你也可以ping另一台局域网中的电脑试试；</p>
</li>
<li>
<p>运行截图</p>
<p><img src="https://whowin.gitee.io/images/180007/screenshot_arp_server.png" alt="screenshot of running arp_server"></p>
</li>
</ul>
<h2 id="欢迎订阅-网络编程专栏httpsblogcsdnnetwhowincategory_12180345html"><strong>欢迎订阅 <a href="https://blog.csdn.net/whowin/category_12180345.html">『网络编程专栏』</a></strong></h2>
<hr>
<p><strong>欢迎访问我的博客：https://whowin.cn</strong></p>
<p><strong>email: <a href="mailto:hengch@163.com">hengch@163.com</a></strong></p>
<p><img src="https://whowin.gitee.io/images/qrcode/sponsor-qrcode.png" alt="donation"></p>
<!-- CSDN
[img01]:https://img-blog.csdnimg.cn/img_convert/cbabdf8b58abc96a519821bbff8d6196.png
[img02]:https://img-blog.csdnimg.cn/img_convert/9338c43a9843115c5309676dafa76713.png
[img03]:https://img-blog.csdnimg.cn/img_convert/0dfca3ed4940a3c01b708c426c066f66.png
[img04]:https://img-blog.csdnimg.cn/img_convert/8ba373f0c0c93c4fc0f42bee3a67b3d3.png
-->
<!--gitee
[article1]:https://whowin.gitee.io/post/blog/network/0002-link-layer-programming/
[article2]:https://whowin.gitee.io/post/blog/network/0006-send-udp-with-raw-socket/
[article3]:https://whowin.gitee.io/post/blog/network/0014-handling-arp-cache/
-->
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">whowin</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2023-01-12
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/linux/">Linux</a>
          <a href="/tags/socket/">Socket</a>
          <a href="/tags/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/">网络编程</a>
          <a href="/tags/arp/">arp</a>
          <a href="/tags/raw-socket/">raw socket</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/blog/network/0009-get-gateway-ip-via-netlink/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">linux下使用netlink获取gateway的IP地址</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/blog/network/0014-handling-arp-cache/">
            <span class="next-text nav-default">如何用C语言操作arp cache</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  <span id="/post/blog/network/0007-recv-arp-request-and-reply/" class="leancloud_visitors" data-flag-title="接收arp请求并发送回应的实例">
		<span class="post-meta-item-text">文章阅读量 </span>
		<span class="leancloud-visitors-count">0</span>
		<p></p>
	  </span>
  <div id="vcomments"></div>
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
  <script type="text/javascript">
    new Valine({
        el: '#vcomments' ,
        appId: 'OFCGzCfJRUglzOdzrqMGkbTR-gzGzoHsz',
        appKey: 'v7P29kPAEbsmaavaYPNhGhnF',
        notify:  false ,
        verify:  false ,
        avatar:'mm',
        placeholder: '说点什么吧...',
        visitor:  true 
    });
  </script>

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:hengch@163.com" class="iconfont icon-email" title="email"></a>
  <a href="https://whowin.gitee.io/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>
<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
      <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    2022 - 
    2024<span class="heart"><i class="iconfont icon-heart"></i></span><span>whowin</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.64437849d125a2d603b3e71d6de5225d641a32d17168a58106e0b61852079683.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        }
    };
  </script>
  <script async src="https://cdn.jsdelivr.net/npm/mathjax@3.0.5/es5/tex-mml-chtml.js" integrity="sha256-HGLuEfFcsUJGhvB8cQ8nr0gai9EucOOaIxFw7qxmd+w=" crossorigin="anonymous"></script>








</body>
</html>
